package com.ChildDoodle;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

class OnColorChangeListener {
	void OnColorChange(int nColor) {

	}
}

public class ColorPalette extends android.widget.ImageView {
	int m_nColor;
	Paint m_Paint;
	Bitmap m_Bitmap;
	int[][] m_nColors;
	int m_nSelectId;
	int m_nColomnWidth;
	int m_nRowHeight;
	OnColorChangeListener m_Listener;

	public ColorPalette(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public ColorPalette(Context context, AttributeSet attrs) {
		super(context, attrs);
		m_Paint = new Paint();
		m_nColors = new int[2][6];
		m_nColors[0][0] = Color.rgb(251, 241, 30);
		m_nColors[0][1] = Color.rgb(0, 0, 0);
		m_nColors[0][2] = Color.rgb(63, 212, 252);
		m_nColors[0][3] = Color.rgb(183, 66, 147);
		m_nColors[0][4] = Color.rgb(109, 192, 86);
		m_nColors[0][5] = Color.rgb(102, 67, 45);
		m_nColors[1][0] = Color.rgb(255, 58, 26);
		m_nColors[1][1] = Color.rgb(255, 148, 102);
		m_nColors[1][2] = Color.rgb(255, 103, 178);
		m_nColors[1][3] = Color.rgb(0, 76, 168);
		m_nColors[1][4] = Color.rgb(52, 48, 133);
		m_nColors[1][5] = Color.rgb(0, 167, 115);

		m_nSelectId = 0;

		m_nColor = m_nColors[0][0];

		this.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
			}
		});

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		int nX;
		int nY;
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			nX = (int) event.getX();
			nY = (int) event.getY();

			int nRow = nY / m_nRowHeight;
			int nColomn = nX / m_nColomnWidth;
			if (nColomn < 0 || nColomn >= 6 || nRow < 0 || nRow >= 2) {
				break;
			}

			m_nSelectId = nRow * 6 + nColomn;
			m_nColor = m_nColors[nRow][nColomn];
			invalidate();

			if (m_Listener != null) {
				m_Listener.OnColorChange(GetColor());
			}
			break;

		}

		return true;
	}

	public int GetColor() {
		return m_nColor;
	}

	void SetOnColorChangeListener(OnColorChangeListener listener) {
		m_Listener = listener;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		int nGap = 3;
		super.onDraw(canvas);

		int nWidth = getWidth();
		int nHeight = getHeight();

		m_nColomnWidth = nWidth / 6;
		m_nRowHeight = nHeight / 2;

		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 6; j++) {
				Rect rcItem = new Rect();

				rcItem.left = j * m_nColomnWidth;
				rcItem.top = i * m_nRowHeight;
				rcItem.right = rcItem.left + m_nColomnWidth;
				rcItem.bottom = rcItem.top + m_nRowHeight;
				m_Paint.setColor(Color.WHITE);

				canvas.drawRect(rcItem, m_Paint);
				m_Paint.setColor(Color.rgb(160, 160, 160));

				m_Paint.setStyle(Style.STROKE);
				canvas.drawRect(rcItem, m_Paint);
				rcItem.left += nGap;
				rcItem.top += nGap;
				rcItem.right -= nGap;
				rcItem.bottom -= nGap;

				m_Paint.setStyle(Style.FILL);
				m_Paint.setColor(m_nColors[i][j]);
				canvas.drawRect(rcItem, m_Paint);

				if (m_nSelectId == i * 6 + j) {
					rcItem.left -= nGap;
					rcItem.top -= nGap;
					rcItem.right += nGap;
					rcItem.bottom += nGap;

					m_Paint.setColor(Color.RED);
					m_Paint.setAlpha(125);

					canvas.drawRect(rcItem, m_Paint);

				}
			}
		}
	}
}